package com.vehiclemanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehiclemanagement.common.result.Result;
import com.vehiclemanagement.entity.OperationLog;
import com.vehiclemanagement.service.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 */
@RestController
@RequestMapping("/operation-log")
@Slf4j
@PreAuthorize("hasRole('ADMIN')")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    /**
     * 分页查询日志
     */
    @GetMapping("/list")
    public Result<IPage<OperationLog>> list(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "module", required = false) String module,
            @RequestParam(value = "operationType", required = false) Integer operationType,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "startTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        log.info("分页查询操作日志: current={}, size={}, username={}, module={}, operationType={}, status={}, startTime={}, endTime={}",
                current, size, username, module, operationType, status, startTime, endTime);

        try {
            IPage<OperationLog> page = operationLogService.pageLogs(
                    current, size, username, module, operationType, status, startTime, endTime);
            return Result.success(page);
        } catch (Exception e) {
            log.error("查询操作日志失败: {}", e.getMessage());
            return Result.error("查询操作日志失败: " + e.getMessage());
        }
    }

    /**
     * 按操作类型统计
     */
    @GetMapping("/statistics/type")
    public Result<List<Map<String, Object>>> statisticsByType(
            @RequestParam(value = "startTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        log.info("按操作类型统计: startTime={}, endTime={}", startTime, endTime);

        try {
            List<Map<String, Object>> result = operationLogService.statisticsByOperationType(startTime, endTime);
            return Result.success(result);
        } catch (Exception e) {
            log.error("统计操作类型失败: {}", e.getMessage());
            return Result.error("统计操作类型失败: " + e.getMessage());
        }
    }

    /**
     * 按模块统计
     */
    @GetMapping("/statistics/module")
    public Result<List<Map<String, Object>>> statisticsByModule(
            @RequestParam(value = "startTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        log.info("按模块统计: startTime={}, endTime={}", startTime, endTime);

        try {
            List<Map<String, Object>> result = operationLogService.statisticsByModule(startTime, endTime);
            return Result.success(result);
        } catch (Exception e) {
            log.error("统计模块失败: {}", e.getMessage());
            return Result.error("统计模块失败: " + e.getMessage());
        }
    }

    /**
     * 按日期统计
     */
    @GetMapping("/statistics/date")
    public Result<List<Map<String, Object>>> statisticsByDate(
            @RequestParam(value = "startTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {

        log.info("按日期统计: startTime={}, endTime={}", startTime, endTime);

        try {
            List<Map<String, Object>> result = operationLogService.statisticsByDate(startTime, endTime);
            return Result.success(result);
        } catch (Exception e) {
            log.error("统计日期失败: {}", e.getMessage());
            return Result.error("统计日期失败: " + e.getMessage());
        }
    }

    /**
     * 清除日志
     */
    @DeleteMapping("/clear/{days}")
    public Result<Integer> clearLogs(@PathVariable Integer days) {
        log.info("清除{}天前的日志", days);

        if (days == null || days <= 0) {
            return Result.validateFailed("清除天数必须大于0");
        }

        try {
            int count = operationLogService.clearLogs(days);
            return Result.success("清除日志成功", count);
        } catch (Exception e) {
            log.error("清除日志失败: {}", e.getMessage());
            return Result.error("清除日志失败: " + e.getMessage());
        }
    }
} 